package com.material.controller;

import com.material.common.annotation.LoginUser;
import com.material.common.annotation.PassToken;
import com.material.common.core.domain.entity.SysUser;
import com.material.common.entity.ResultResponse;
import com.material.common.utils.JwtUtil;
import com.material.common.utils.SecurityUtils;
import com.material.common.utils.bean.BeanUtil;
import com.material.factory.domain.FactoryInfo;
import com.material.factory.service.IFactoryInfoService;
import com.material.request.LoginRequest;
import com.material.response.LoginResponse;
import com.material.response.UserInfoResponse;
import com.material.supplier.domain.SupplierInfo;
import com.material.supplier.service.ISupplierInfoService;
import com.material.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author ganxinge
 */
@Api(tags = "1.用户登录")
@RestController
public class LoginController {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISupplierInfoService supplierInfoService;
    @Autowired
    private IFactoryInfoService factoryInfoService;

    @ApiOperation("登录")
    @PostMapping("/login")
    @PassToken
    public ResultResponse login(@RequestBody LoginRequest loginRequest){
        SysUser sysUser = sysUserService.selectUserByUserName(loginRequest.getUserName());
        if(sysUser == null){
            return ResultResponse.error("用户不存在！");
        }
        Integer type = loginRequest.getType();
        if(LoginRequest.TYPE_1.equals(type)){
            String pwd = SecurityUtils.encryptPassword(loginRequest.getPassword());
            if(!pwd.equals(sysUser.getPassword())){
                return ResultResponse.error("密码错误");
            }
        }

        String userType = sysUser.getUserType();
        if(!(SysUser.USER_TYPE_3.equals(userType)||SysUser.USER_TYPE_2.equals(userType)||SysUser.USER_TYPE_1.equals(userType))){
            return ResultResponse.error("当前用户无法登录，请联系管理员");
        }

        String sign = JwtUtil.sign(loginRequest.getUserName(), sysUser.getPassword());
        LoginResponse response = new LoginResponse();
        if(SysUser.USER_TYPE_3.equals(userType)){
            SupplierInfo supplierInfo = supplierInfoService.selectSupplierInfoById(sysUser.getBizId());
            if(supplierInfo == null){
                return ResultResponse.success("供应商数据异常，请联系管理员");
            }
            response.setDisplayName(supplierInfo.getName());
            response.setAuthStatus(supplierInfo.getAuthStatus());
        }
        if(SysUser.USER_TYPE_1.equals(userType) || SysUser.USER_TYPE_2.equals(userType)){
            FactoryInfo factoryInfo = factoryInfoService.selectFactoryInfoById(sysUser.getBizId());
            response.setDisplayName(factoryInfo.getName());
        }
        response.setToken(sign);
        response.setUserType(sysUser.getUserType());
        response.setAvatar(sysUser.getAvatar());
        response.setNickName(sysUser.getNickName());
        response.setUserName(sysUser.getUserName());
        return ResultResponse.success(response);
    }

    @ApiOperation("获取登录用户信息")
    @GetMapping("/userInfo")
    public ResultResponse getLoginUser(@ApiIgnore @LoginUser SysUser sysUser){
        String userType = sysUser.getUserType();
        UserInfoResponse userInfoResponse = BeanUtil.copyProperties(sysUser, UserInfoResponse.class);
        if(SysUser.USER_TYPE_3.equals(userType)){
            SupplierInfo supplierInfo = supplierInfoService.selectSupplierInfoById(sysUser.getBizId());
            if(supplierInfo == null){
                return ResultResponse.success("供应商数据异常，请联系管理员");
            }
            userInfoResponse.setDisplayName(supplierInfo.getName());
        }
        if(SysUser.USER_TYPE_1.equals(userType) || SysUser.USER_TYPE_2.equals(userType)){
            FactoryInfo factoryInfo = factoryInfoService.selectFactoryInfoById(sysUser.getBizId());
            userInfoResponse.setDisplayName(factoryInfo.getName());
        }
        return ResultResponse.success(userInfoResponse);
    }
}
